package main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;

import javax.swing.JOptionPane;

public class CreateTempFileClass extends CreatePolicyMain implements
        Serializable
{
    private File objA = null;
    private File objW = null;
    private File[] FileArray = new File[18];
    private FileWriter FileWArray[] = new FileWriter[18];
    private File TempPath = new File("C:\\");
    private String unit;

    public CreateTempFileClass()
    {
    }

    public void CreateTempFile(File obj)
    {
        String Line , line1 , line2 , line3 , line4 , line5 , line6 = null;
        String Pkind = null;
        File ROOT = new File("C:\\");
        commit = 1;
        try
        {
	  fileA = File.createTempFile("住火保單",".tmp",ROOT);
	  fileW = File.createTempFile("屋主保單",".tmp",ROOT);
	  fw1 = new FileWriter(fileA);
	  fw2 = new FileWriter(fileW);
	  isr = new InputStreamReader(new FileInputStream(obj.getPath()),"ISO8859_1");
	  br = new BufferedReader(isr);

	  while ( ( Line = br.readLine() )!=null)
	  {
	      // Key Value of type
	      Pkind = Line.substring(425,426);
	      commit = commit+1;
	      // Change to Chinese
	      line1 = new String(Line.substring(40,70).getBytes("ISO8859_1"),"MS950");
	      line2 = new String(Line.substring(156,216).getBytes("ISO8859_1"),"MS950");
	      line3 = new String(Line.substring(226,306).getBytes("ISO8859_1"),"MS950");
	      line4 = new String(Line.substring(376,406).getBytes("ISO8859_1"),"MS950");
	      line5 = new String(Line.substring(461,473).getBytes("ISO8859_1"),"MS950");
	      line6 = new String(Line.substring(482,494).getBytes("ISO8859_1"),"MS950");
	      // **************************Change Text Length*******
	      Line = Line.substring(0,40)+line1+Line.substring(70,156)+line2+Line.substring(216,226)+line3+Line.substring(306,376)+line4+Line.substring(406,461)+line5+Line.substring(473,482)+line6;
	      // System.out.println(line2);
	      // System.out.println(line3);
	      // Write Temp File
	      if(Pkind.equals("A"))
	      {
		fw1.write(Line+"\r\n");
		objA = fileA;

	      }
	      else if(Pkind.equals("W"))
	      {
		fw2.write(Line+"\r\n");
		objW = fileW;
	      }
	  }
	  fw1.close();
	  fw2.close();
	  br.close();
	  isr.close();
	  // Recovery temp file
	  if(fileW==null||fileW.length()==0)
	      fileW.delete();
	  if(fileA==null||fileA.length()==0)
	      fileA.delete();

        }
        catch (IOException e)
        {
	  try
	  {
	      fw1.close();
	      fw2.close();
	      br.close();
	      isr.close();
	  }
	  catch (IOException e1)
	  {
	      System.out.println("CreateTempFile IOException e1");
	      ErrorMessage(e1);
	  }
	  deleteFile(fileA);
	  deleteFile(fileW);
	  ErrorMessage(e);
        }
        catch (java.lang.StringIndexOutOfBoundsException e)
        {
	  try
	  {
	      fw1.close();
	      fw2.close();
	      br.close();
	      isr.close();
	  }
	  catch (IOException e1)
	  {
	      System.out.println("CreateTempFile java.lang.StringIndexOutOfBoundsException e");
	      ErrorMessage(e1);
	  }
	  deleteFile(fileA);
	  deleteFile(fileW);
	  ErrorMessage(e);
        }

    }

    // public File[] CreateUnitFile(File obj)
    // {
    // try
    // {
    // FileArray[0] = File.createTempFile("00台北@",".tmp",TempPath);
    // FileArray[1] = File.createTempFile("01桃園@",".tmp",TempPath);
    // FileArray[2] = File.createTempFile("02台中@",".tmp",TempPath);
    // FileArray[3] = File.createTempFile("03台南@",".tmp",TempPath);
    // FileArray[4] = File.createTempFile("04高雄@",".tmp",TempPath);
    // FileArray[5] = File.createTempFile("05彰化@",".tmp",TempPath);
    // FileArray[6] = File.createTempFile("06羅東@",".tmp",TempPath);
    // FileArray[7] = File.createTempFile("13嘉義@",".tmp",TempPath);
    // FileArray[8] = File.createTempFile("32新竹@",".tmp",TempPath);
    // FileArray[9] = File.createTempFile("36屏東@",".tmp",TempPath);
    // FileArray[10] = File.createTempFile("43苗栗@",".tmp",TempPath);
    // FileArray[11] = File.createTempFile("45斗六@",".tmp",TempPath);
    // FileArray[12] = File.createTempFile("46新營@",".tmp",TempPath);
    // FileArray[13] = File.createTempFile("51板橋@",".tmp",TempPath);
    // FileArray[14] = File.createTempFile("55新莊@",".tmp",TempPath);
    // FileArray[15] = File.createTempFile("85中山@",".tmp",TempPath);
    // FileArray[16] = File.createTempFile("98花蓮@",".tmp",TempPath);
    // FileArray[17] = File.createTempFile("99南投@",".tmp",TempPath);
    //
    // for(int i = 0; i<FileArray.length; i++)
    // {
    // FileWArray[i] = new FileWriter(FileArray[i]);
    // }
    // isr = new InputStreamReader(new
    // FileInputStream(obj.getPath()),"ISO8859_1");
    // br = new BufferedReader(isr);
    //
    // while ( ( Line = br.readLine() )!=null)
    // {
    //
    // unit = Line.substring(421,423); // 保單編碼
    // System.out.println(unit);
    // // 轉中文
    // line1 = new String(Line.substring(40,70).getBytes("ISO8859_1"),"MS950");
    // line2 = new
    // String(Line.substring(156,216).getBytes("ISO8859_1"),"MS950");
    // line3 = new
    // String(Line.substring(226,306).getBytes("ISO8859_1"),"MS950");
    // line4 = new
    // String(Line.substring(376,406).getBytes("ISO8859_1"),"MS950");
    // line5 = new
    // String(Line.substring(461,473).getBytes("ISO8859_1"),"MS950");
    // line6 = new
    // String(Line.substring(482,494).getBytes("ISO8859_1"),"MS950");
    // // **************************Change Text Length*******
    //
    // }
    // for(int i = 0; i<FileWArray.length; i++)
    // {
    // FileWArray[i].close();
    // }
    // br.close();
    // isr.close();
    // }
    // catch (IOException e1)
    // {
    // JOptionPane.showMessageDialog(null,e1.toString()+" 檔案有問題！\n 請重新選擇檔案！");
    // }
    // return FileArray;
    //
    // }

    public File GetAtype()
    {
        return objA;
    };

    public File GetWtype()
    {
        return objW;
    }
}
